﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Link_eLab.SqlSugar.Base
{
    public interface IBaseDao<T>
    {
        /// <summary>
        /// 插入数据 返回自增主键
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns>JSON结果集</returns>
        Task<int> Insert(T entity);

        /// <summary>
        /// 批量插入
        /// </summary>
        /// <param name="entitys"></param>
        /// <returns></returns>
        Task<int> InsertMany(List<T> entitys);

        /// <summary>
        /// 根据主键删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns>JSON结果集</returns>
        Task<int> Delete(string id);

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="whereExpression">条件表达式</param>
        /// <returns>JSON结果集</returns>
        Task<int> Delete(Expression<Func<T, bool>> whereExpression);

        /// <summary>
        /// 更新(根据主键)
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns>JSON结果集</returns>
        Task<int> Update(T entity);

        /// <summary>
        /// 批量更新(根据主键)
        /// </summary>
        /// <param name="entity">实体对象List</param>
        /// <returns>JSON结果集</returns>
        Task<int> Update(List<T> entity);

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="updateColumns">更新字段</param>
        /// <param name="whereExpression">条件表达式</param>
        /// <returns>JSON结果集</returns>
        Task<int> Update(Expression<Func<T, T>> updateColumns, Expression<Func<T, bool>> whereExpression);

        /// <summary>
        /// 按条件查询一个
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>JSON结果集</returns>
        Task<T> QuerySingle(string id);

        /// <summary>
        /// 按条件查询一个
        /// </summary>
        /// <param name="whereExpression">条件表达式</param>
        /// <returns>JSON结果集</returns>
        Task<T> QuerySingle(Expression<Func<T, bool>> whereExpression);

        /// <summary>
        /// 查询所有
        /// </summary>
        /// <returns>JSON结果集</returns>
        Task<List<T>> QueryAll();

        /// <summary>
        /// 按条件查询
        /// </summary>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="orderExpression">排序表达式</param>
        /// <param name="orderType">排序方式</param>
        /// <returns>JSON结果集</returns>
        Task<List<T>> QueryByWhere(Expression<Func<T, object>> whereExpression,
            Expression<Func<T, object>> orderExpression = null, OrderByType orderType = OrderByType.Asc);

        /// <summary>
        /// 按条件查询
        /// </summary>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="orderExpression">排序表达式</param>
        /// <param name="orderType">排序方式</param>
        /// <returns>JSON结果集</returns>
        Task<List<T>> QueryByWhere(Expression<Func<T, bool>> whereExpression,
            Expression<Func<T, object>> orderExpression = null, OrderByType orderType = OrderByType.Asc);

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="orderExpression">排序表达式</param>
        /// <param name="orderType">排序方式</param>
        /// <returns>JSON结果集</returns>
        Task<List<T>> QueryByWherePage(int pageIndex, int pageSize, Expression<Func<T, object>> whereExpression,
            Expression<Func<T, object>> orderExpression = null, OrderByType orderType = OrderByType.Asc);

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="orderExpression">排序表达式</param>
        /// <param name="orderType">排序方式</param>
        /// <returns>JSON结果集</returns>
        Task<List<T>> QueryByWherePage(int pageIndex, int pageSize, Expression<Func<T, bool>> whereExpression,
            Expression<Func<T, object>> orderExpression = null, OrderByType orderType = OrderByType.Asc);

        /// <summary>
        /// 查询条数
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="sql">SQL语句</param>
        /// <param name="whereObj">条件表达式</param>
        /// <returns></returns>
        Task<int> QueryCount<TResult>(string sql, object whereObj = null);

        /// <summary>
        /// 查询条数
        /// </summary>
        /// <param name="whereExpression">条件表达式</param>
        /// <returns>JSON结果集</returns>
        Task<int> QueryCount(Expression<Func<T, object>> whereExpression);

        /// <summary>
        /// 查询条数
        /// </summary>
        /// <param name="whereExpression">条件表达式</param>
        /// <returns>JSON结果集</returns>
        Task<int> QueryCount(Expression<Func<T, bool>> whereExpression);

        /// <summary>
        /// 查询条数
        /// </summary>
        /// <param name="searchParam">条件表达式</param>
        /// <returns>JSON结果集</returns>
        Task<int> QueryCount(List<IConditionalModel> searchParam);

        /// <summary>
        /// 查询是否存在记录
        /// </summary>
        /// <param name="whereExpression">条件表达式</param>
        /// <returns>JSON结果集</returns>
        Task<bool> Any(Expression<Func<T, bool>> whereExpression);

        /// <summary>
        /// 执行sql语句或存储过程
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="sql">sql语句</param>
        /// <param name="whereObj">命令参数对应匿名对象</param>
        /// <returns>JSON结果集</returns>
        Task<List<TResult>> QueryBySql<TResult>(string sql, object whereObj = null);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="whereObj"></param>
        /// <returns>JSON结果集</returns>
        Task<int> ExecuteBySql(string sql, object whereObj = null);

        /// <summary>
        /// 事务执行sql语句或存储过程
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="sql">sql语句</param>
        /// <param name="whereObj">命令参数对应匿名对象</param>
        /// <returns>JSON结果集</returns>
        Task<List<TResult>> QueryBySqlTransactions<TResult>(string sql, object whereObj = null);

    }
}